<template>
    <div class="container">
        <h1 class="text-center">Изменения данных заказчика</h1>
        <b-form @submit.prevent="updateCustomer">
            <b-form-group  id="fieldset-last-name" label="Введите Фамилию:" label-for="input-1">
                <b-form-input
                    id="input-1"
                    v-model="lastName"
                    type="text"
                    required
                ></b-form-input>
            </b-form-group>

            <b-form-group  id="fieldset-first-name" label="Введите Имя:" label-for="input-1">
                <b-form-input
                    id="input-1"
                    v-model="firstName"
                    type="text"
                    required
                ></b-form-input>
            </b-form-group>

            <b-form-group  id="fieldset-patronymic" label="Введите Отчество:" label-for="input-1">
                <b-form-input
                    id="input-1"
                    v-model="patronymic"
                    type="text"
                    required
                ></b-form-input>
            </b-form-group>

            <b-form-group id="fieldset-2" label-for="input-2">
                <b-form-checkbox
                    id="input-2"
                    v-model="legalEntity"
                    type="text"
                    value="true"
                    unchecked-value="false"
                    >Юридическое лицо</b-form-checkbox
                >
            </b-form-group>

            <b-form-group id="fieldset-3" label="Адрес:" label-for="input-3">
                <b-form-input
                    id="input-3"
                    v-model="address"
                    type="text"
                    required
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-4"
                label="Ввеедие нормер телефона:"
                label-for="input-4"
            >
                <b-form-input
                    id="input-4"
                    type="tel"
                    v-model="phoneNumber"
                    required
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-5"
                label="Ввеедие email:"
                label-for="input-5"
            >
                <b-form-input
                    id="input-5"
                    v-model="email"
                    type="email"
                    required
                ></b-form-input>
            </b-form-group>
            <b-button type="submit" variant="primary">Изменить</b-button>
        </b-form>
        <b-button @click="deleteCustomer" variant="primary">Удалить заказчика</b-button>
        <h5>{{ notification }}</h5>
    </div>
</template>

<script>
export default {
    data() {
        return {
            legalEntity: "",
            firstName: "",
            lastName: "",
            patronymic: "",
            address: "",
            phoneNumber: "",
            email: "",
            notification: ""
        };
    },
    methods: {
        async updateCustomer() {
            await this.$http.put("/customer/update/" + this.$route.params.id, {
                  "legal_entity": this.legalEntity,
                  "first_name": this.firstName,
                  "last_name": this.lastName,
                  "patronymic": this.patronymic,
                  "address": this.address,
                  "phone_number": this.phoneNumber,
                  "email": this.email
            })
            .then(response => {
                console.log(response)
                this.$router.push({path: "/customer"}) 
            })
            .catch(e => {
                this.errors.push(e)
            })
        },
        async deleteCustomer() {
            await this.$http.delete("/customer/delete/" + this.$route.params.id)
                .then(response => {
                    console.log(response)
                    this.$router.push({path: "/customer"}) 
                })
                .catch(e => {
                    this.notification = "Вы не можете удалить заказчика, который заказал экспертизу, сначала удалите экспертизу в которой он состоит"
                    this.errors.push(e)
                })
        }
    },
    created() {
        this.$http.get("/customer/" + this.$route.params.id)
            .then(response => {
                this.legalEntity = response.data.legal_entity,
                this.firstName = response.data.first_name,
                this.lastName = response.data.last_name,
                this.patronymic = response.data.patronymic,
                this.address = response.data.address,
                this.phoneNumber = response.data.phone_number,
                this.email = response.data.email
            })
    }
};
</script>
<style>
</style>
